package com.tul.exercise.base.java8.stream.XxxStream;

import com.tul.exercise.base.java8.stream.base.Apple;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;

/**
 * @ClassName IntStreamTest
 * @Description
 * @Author tul 2019/1/8
 * @Version 1.0
 **/
public class IntStreamTest {
    @Test
    @DisplayName("XxxStream操作")
    public void test01(){
        List<Apple> appleList = Apple.createApples(100);

        // 求总结
        DoubleStream weightStream = appleList.stream()
                .mapToDouble(apple->apple.getWeight());
        System.out.println(weightStream.summaryStatistics());


        // 求最大值
        OptionalDouble max = appleList.stream()
                .mapToDouble(apple->apple.getWeight())
                .max();
        System.out.println("max=" + max.getAsDouble());


        // 求最小值
        OptionalDouble min = appleList.stream()
                .mapToDouble(apple->apple.getWeight())
                .min();
        System.out.println("min=" + min.getAsDouble());


        // 求均值
        OptionalDouble avg = appleList.stream()
                .mapToDouble(apple->apple.getWeight())
                .average();
        System.out.println("avg=" + avg.getAsDouble());

        // 计数
        long count = appleList.stream()
                .mapToDouble(apple->apple.getWeight())
                .count();
        System.out.println("count=" + count);

    }
}
